package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;

import dialogs.DeleteTupleDialog;
import dialogs.InsertTupleDialog;

import models.LeadSingerModel;
import system.View;

public class LeadSingerController extends AbstractMenuItemController {
	private LeadSingerModel lead_singer = new LeadSingerModel();

	public LeadSingerController(View v) {
		super(v);
		abstractModel = lead_singer;
		lead_singer.addExceptionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Dissasociate Lead Singer"))
			showDialogWindow(new DeleteTupleDialog(view, actionCommand, "Key",
					new String[] { "Item UPC: ", "Lead Singer Name: " }, this));
		else if (actionCommand.equals("Set Lead Singer"))
			showDialogWindow(new InsertTupleDialog(view, actionCommand,
					"Lead Singer", new String[] { "Item UPC: ",
							"Lead Singer Name: " }, this));
	}

	@Override
	public int validateInsert(Vector<String> s) {
		if (s.size() != 2)
			return VALIDATIONERROR;

		try {
			Integer upc;
			String name;

			if (s.get(0).trim().length() != 0)
				upc = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			if (s.get(1).trim().length() != 0)
				name = s.get(1).trim();
			else return VALIDATIONERROR;

			view.updateStatusBar("Setting Lead Singer...");

			if (lead_singer.insert(upc, name)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}

	@Override
	public int validateDelete(Vector<String> s) {
		if (s.size() != 2)
			return VALIDATIONERROR;

		try {
			Integer upc;
			String name;

			if (s.get(0).trim().length() != 0)
				upc = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			if (s.get(1).trim().length() != 0)
				name = s.get(1).trim();
			else return VALIDATIONERROR;

			view.updateStatusBar("Disassociating Lead Singer...");

			if (lead_singer.delete(upc, name)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}
}
